﻿//Copyright Notice:  ©2009 Microsoft Corporation.  All rights reserved.
using System;
using System.Runtime.Serialization;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Data;

namespace Microsoft.InformationSecurity.CISF.Security.AuthZServices
{
    /// <summary>
    /// Gets a collection of User objects from cache.
    /// </summary>
    internal class GetUsers
    {
        #region Private Variables;
        private Collection<User> users;

        #endregion

        #region Internal Variables
        internal enum QueryType
        {
            Role = 1,
            Alias = 2
        }

        #endregion

        #region Constructor
        /// <summary>
        /// Gets a collection of User objects from cache.
        /// </summary>
        /// <param name="queryParameter">The column value to query on.</param>
        /// <param name="queryType">The column type to query with.</param>
        internal GetUsers(string queryParameter, QueryType queryType)
        {
            string dbName = "RTDB";
            string [] roles = null;
            SqlQuery sqlQuery = null;
            Collection<SqlParameter> sqlParameters = new Collection<SqlParameter>();            
            this.users = new Collection<User>();

            if (queryType == QueryType.Role)
            {
                sqlParameters.Add(new SqlParameter("@role", SqlDbType.NVarChar, 50, queryParameter));
                sqlQuery = new SqlQuery("AuthZ_spGetUsersByRole", CommandType.StoredProcedure, sqlParameters);
            }
            else
            {
                sqlParameters.Add(new SqlParameter("@alias", SqlDbType.NVarChar, 50, queryParameter));
                sqlQuery = new SqlQuery("AuthZ_spGetUserByAlias", CommandType.StoredProcedure, sqlParameters);
            }

            //For each user returned by the query.
            foreach (object[] columns in AuthZDataAccess.GetSqlDataReader(dbName, sqlQuery))
            {
                User user = new User();

                user.PersonId = Int32.Parse(columns[0].ToString());
                user.FullName = columns[1].ToString();
                user.EMailName = columns[2].ToString();
                user.DisplayName = columns[3].ToString();
                user.PersonnelNumber = Int32.Parse(columns[4].ToString());
                user.Alias = columns[5].ToString();

                if (queryType != QueryType.Role)
                {
                    roles = columns[6].ToString().Split(',');                 
                    user.Roles = new string[roles.Length];
                    int idx = 0;

                    //For each role returned by the query.
                    foreach (string role in roles)
                    {
                        user.Roles[idx++] = role.Trim();
                    }
                }                
                users.Add(user);
            }
        }

        #endregion

        #region Properties

        #region Users
        /// <summary>
        /// Gets the collection of User objects.
        /// </summary>
        public Collection<User> Users
        {
            get { return this.users; }
        }

        #endregion

        #endregion

    }
}
